package cn.laixueit.controller;

import cn.laixueit.pojo.Goods;
import cn.laixueit.pojo.Sku;
import cn.laixueit.pojo.Spu;
import cn.laixueit.pojo.SpuDetail;
import cn.laixueit.result.PageResult;
import cn.laixueit.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SpuController {

    @Autowired
    private SpuService spuService;

    @GetMapping("/spu/page")
    public ResponseEntity<PageResult> spuPage(@RequestParam(value = "key",required = false)String key,
                                              @RequestParam(value = "saleable",required = false)Boolean saleable,
                                              @RequestParam("page")Integer page,
                                              @RequestParam("rows")Integer rows){
        PageResult pageResult =  spuService.spuPage(key,saleable,page,rows);
        return ResponseEntity.ok(pageResult);
    }

    @PostMapping("/goods")
    public ResponseEntity<Void> saveGoods(@RequestBody Goods goods){
        spuService.saveGoods(goods);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    @GetMapping("/sku/skuListBySpuId/{spuId}")
    public ResponseEntity<List<Sku>> skuListBySpuId(@PathVariable("spuId")Long spuId){
        List<Sku> skuList = spuService.skuListBySpuId(spuId);
        return ResponseEntity.ok(skuList);
    }

    @GetMapping("/spuDetail/{id}")
    public ResponseEntity<SpuDetail> spuDetail(@PathVariable("id")Long id){
       SpuDetail spuDetail =   spuService.spuDetail(id);
       return ResponseEntity.ok(spuDetail);
    }

    @GetMapping("/spu/{id}")
    public ResponseEntity<Spu> getSpuById(@PathVariable("id")Integer id){
        Spu spu = spuService.getById(id);
        return ResponseEntity.ok(spu);
    }
}
